from PySide import QtGui
import g_view
import b_material

class EditMaterials(QtGui.QWidget):
	
	def __init__(self, parent):
		super(EditMaterials, self).__init__()
		self.parent = parent
		self.errormsg = QtGui.QErrorMessage()
		self.initUI()

	def initUI(self):
		self.listmaterials = QtGui.QListWidget()
		self.listmaterials.addItems(self.parent.data.materials.keys())
		self.listmaterials.sortItems()
		self.listmaterials.itemSelectionChanged.connect(self.selectionChanged)
		
		row = 0
		grid = QtGui.QGridLayout()
		name = QtGui.QLabel('Name')
		self.nameedit = QtGui.QLineEdit()
		grid.addWidget(name, row, 0)
		grid.addWidget(self.nameedit, row, 1)
		row += 1
		area = QtGui.QLabel('Area')
		self.areaedit = QtGui.QLineEdit()
		grid.addWidget(area, row, 0)
		grid.addWidget(self.areaedit, row, 1)
		row += 1
		youngsmodulus = QtGui.QLabel('Youngs Modulus')
		self.youngsmodulusedit = QtGui.QLineEdit()
		grid.addWidget(youngsmodulus, row, 0)
		grid.addWidget(self.youngsmodulusedit, row, 1)
		row += 1
		density = QtGui.QLabel('Density')
		self.densityedit = QtGui.QLineEdit()
		grid.addWidget(density, row, 0)
		grid.addWidget(self.densityedit, row, 1)
		row += 1
		
		
		self.addButton = QtGui.QPushButton("Add Material")
		self.addButton.clicked.connect(self.addMaterial)
		self.modifyButton = QtGui.QPushButton("Modify Material")
		self.modifyButton.clicked.connect(self.modifyMaterial)
		self.deleteButton = QtGui.QPushButton("Delete Material")
		self.deleteButton.clicked.connect(self.deleteMaterial)
		buttons = QtGui.QHBoxLayout()
		buttons.addWidget(self.addButton)
		buttons.addWidget(self.modifyButton)
		buttons.addWidget(self.deleteButton)
		
		editdialog = QtGui.QVBoxLayout()
		editdialog.addLayout(grid)
		editdialog.addStretch(1)
		editdialog.addLayout(buttons)
		
		hbox = QtGui.QHBoxLayout()
		hbox.addWidget(self.listmaterials)
		hbox.addLayout(editdialog)
		
		self.setLayout(hbox)
		self.setWindowTitle("Edit Materials")
		
	def closeEvent(self, event):
		self.hide()
	
	def modifyMaterial(self):
		current = self.listmaterials.currentItem()
		if current is not None and current.isSelected():
			name = str(current.text())
			material = self.parent.data.materials[name]
			material.area = float(self.areaedit.text())
			material.youngsmodulus = float(self.youngsmodulusedit.text())
			material.density = float(self.densityedit.text())
			nameedit = str(self.nameedit.text())
			if nameedit != name:
				if nameedit in self.parent.data.materials.keys():
					self.errormsg.showMessage("Material by that name already exists")
					return
				material.name = nameedit
				self.parent.data.materials[nameedit] = material
				material.deleteMaterial()
				self.parent.updateAll()
			else:
				self.parent.updateAll(self)
		else:
			self.errormsg.showMessage("No material selected to modify")
	
	def addMaterial(self):
		tempmaterial = b_material.Material(self.parent.data, self.nameedit.text(), float(self.areaedit.text()), float(self.youngsmodulusedit.text()), float(self.densityedit.text()))
		if tempmaterial.name in self.parent.data.materials:
			self.errormsg.showMessage("Material by that name already exists, cannot add")
		else:
			self.parent.data.materials[tempmaterial.name] = tempmaterial
			self.parent.updateAll(self)
	
	def deleteMaterial(self):
		current = self.listmaterials.currentItem()
		if current is not None and current.isSelected():
			name = str(current.text())
			material = self.parent.data.materials[name]
			for member in self.parent.data.members.values():
				if member.material is material:
					self.parent.layout.view.deleteMember(member)
			material.deleteMaterial()
			self.parent.updateAll(self)
		else:
			self.errormsg.showMessage("No material selected to delete")
	
	def selectionChanged(self):
		current = self.listmaterials.currentItem()
		if current is not None and current.isSelected():
			name = current.text()
			self.nameedit.setText(name)
			material = self.parent.data.materials[name]
			self.areaedit.setText(str(material.area))
			self.youngsmodulusedit.setText(str(material.youngsmodulus))
			self.densityedit.setText(str(material.density))
	
	def updateMaterials(self, check=None):
		if check is self:
			self.updateList()
	
	def updateList(self):
		self.listmaterials.itemSelectionChanged.disconnect(self.selectionChanged)
		self.listmaterials.clear()
		self.listmaterials.addItems(self.parent.data.materials.keys())
		self.listmaterials.sortItems()
		self.listmaterials.itemSelectionChanged.connect(self.selectionChanged)
